package com.shanghaionstar.carmanage.presenter;

import android.content.Context;

import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.enumtype.PackageType;
import com.shanghaionstar.carmanage.iview.IOrderRecordView;
import com.shanghaionstar.carmanage.model.OrderRecordResponseEntity;
import com.shanghaionstar.carmanage.service.OrderRecodService;

import rx.functions.Action1;

/**
 * Created by Yonglang Hong on 2016/7/29.
 */
public class OrderRecordPresenter extends BasePresenter {

    OrderRecodService  recodService;

    IOrderRecordView   recordView;

    public void setReordView(IOrderRecordView recordView) {
        this.recordView = recordView;
    }

    public OrderRecordPresenter(IMVPScheduler mvpScheduler, Context context) {
        super(mvpScheduler);
        recodService = new OrderRecodService(context);
    }

    /**
     * 查询套餐订单信息
     */
    public void loadOrderRecords(PackageType packageTypes, final Integer pageNum, Integer pageSize){
        subscription = recodService.getOrderRecords(packageTypes,pageNum,pageSize)
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<OrderRecordResponseEntity>() {
                    @Override
                    public void call(OrderRecordResponseEntity responseEntity) {
                        recordView.showMoreLayout(responseEntity.hasNextPage());
                        if (pageNum <= 1
                                && (responseEntity.getOrderList()== null || responseEntity.getOrderList().isEmpty())){
                            recordView.showEmpty();
                        } else recordView.showOrderRecordList(responseEntity.getOrderList());
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        recordView.showErr(throwable);
                    }
                });
    }
}
